/*
 * @Author: AskMeWhy
 * @Date:   2020-05-07 17:47:53
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2020-12-29 10:13:01
 */
import { basicBehavior } from "./common";
import { merge } from "../utils/index";

export const AskComponent = options => {
    const oldOptions = {
        methods: {
            observeProps() {
                const _options = merge(true, this.data.options, this.data.props);
                this.setData({ options: _options }, () => {
                    options.methods && options.methods.observeProps && options.methods.observeProps.call(this);
                });
            },
            insertOption(keys = []) {
                const opt = {};
                keys.forEach(item => {
                    opt["options." + item.key] = item.value;
                });
                this.setData(opt, () => {
                    options.methods && options.methods.insertOption && options.methods.insertOption.call(this, keys);
                });
            },
            getPage(){
                let pageList = getCurrentPages();
                return pageList[pageList.length - 1] || null;
            }
        },
    };
    let askOption = {
        options: {
            styleIsolation: "shared",
            multipleSlots: true,
        },
        behaviors: [basicBehavior],
        properties: {
            props: {
                type: Object,
                value: {},
                observer: "observeProps",
            },
        },
        data: {
            parent: null,
            children: [],
            options: {},
        },
        methods: {},
    };
    // options = handleMixins(options);
    options.behaviors = askOption.behaviors.concat(options.behaviors || []);
    askOption = merge(true, {}, askOption, options, oldOptions);
    askOption.lifetimes = {
        attached(payLoad) {
            this.observeProps();
            options.lifetimes && options.lifetimes.attached && options.lifetimes.attached.call(this, payLoad);
        },
        detached(payLoad) {
            options.lifetimes && options.lifetimes.detached && options.lifetimes.detached.call(this, payLoad);
        },
    };
    Component(askOption);
};
